package com.pzl.demo_repository;

import android.graphics.Rect;
import android.os.SystemClock;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.Random;

/**
 * Created by zl.peng on 2016-02-23.
 */
public class MyAdapter extends RecyclerView.Adapter {
    private String[] data = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"};

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new MyHolder(item);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        bindData(holder, position);
        bindListener(holder, position);
    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    /**
     * 绑定监听
     *
     * @param holder
     * @param position
     */
    private void bindListener(RecyclerView.ViewHolder holder, final int position) {
        ((MyHolder) holder).getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "item:" + position, Toast.LENGTH_SHORT).show();
            }
        });

        ((MyHolder) holder).getIv_icon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "icon:" + position, Toast.LENGTH_SHORT).show();
            }
        });

        ((MyHolder) holder).getTv_text2().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "text2:" + position, Toast.LENGTH_SHORT).show();
            }
        });
        ((MyHolder) holder).getRoot().setOnLongClickListener(new View.OnLongClickListener() {//useless
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(v.getContext(), "item onLongClick :" + position, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    /***
     * 绑定数据
     *
     * @param holder
     * @param position
     */
    private void bindData(RecyclerView.ViewHolder holder, final int position) {
        ((MyHolder) holder).getTv_content().setText(data[position]);
    }

}
